const fs = require('fs');

const path = require('path');

const argv = process.argv;

const cwd = process.cwd();

const message = require('./ansi');

let [, , command1, command2, command3] = argv;

const GET_CONFIG = 'getConfig',
  GET_VERSION_FILE = 'getVersionFile',
  GET_VERSION_LOG = 'getVersionLog',
  CONF_TYPE = 'conf',
  HELP = 'help';

/**
 * 加工命令
 * @param {*} command 命令字符串
 */
function commandFactory(command) {
  if (command.includes('=')) {
    return command.split('=')
  }
  return [command]
}

/**
 * 处理命令
 * @param {*} command 命令字符串
 */
function processCommand(command) {

  const params= commandFactory(command);

  switch (params[0]) {

    case GET_CONFIG:
      
      getConfig(params[params.length-1]);

      break;

    case GET_VERSION_FILE:

      getVersionFile(params[params.length-1]);

      break;

    case GET_VERSION_LOG:

      getVersionLog(params[params.length-1]);

      break;

    case HELP:

      const str = `autopack ${GET_CONFIG}=conf  #获取autopack.conf配置文件\n`+
      `autopack ${GET_CONFIG}=js  #获取autopack.config.js配置文件\n`+
      `autopack ${GET_VERSION_FILE}=s  #获取s_version.json s系列版本文件\n`+
      `autopack ${GET_VERSION_FILE}=p  #获取p_version.json p系列版本文件\n`+
      `autopack ${GET_VERSION_LOG}  #获取version.log 版本日志文件\n`+
      `autopack ${HELP}  #获取帮助信息`

      console.log(message.yellow(str))

      break

    default:

      message.error('未找到该命令');

      break;

  }

}


function getConfig(type){

  let filePath,content,filename;

  if(type === CONF_TYPE) {

    filePath = '../default.conf';

    content = readFile(filePath);
  
    filename = 'autopack.conf';

  }else {

    filePath = '../lib/autopack.config.js';

    content = readFile(filePath);
  
    filename = 'autopack.config.js';

  }

  writeFile('/'+filename,content,filename);

}


function getVersionFile(type){

  let filePath,content,filename;

  if(type === 's'){

    filePath = '../lib/s_version.json';

    content = readFile(filePath);
  
    filename = 's_version.json';

  } else {

    filePath = '../lib/p_version.json';

    content = readFile(filePath);
  
    filename = 'p_version.json';

  }

  writeFile('/'+filename,content,filename);

}

function getVersionLog(){

  const filePath = '../logs/version.log';

  const content = readFile(filePath);

  const filename = 'version.log';

  writeFile('/'+filename,content,filename);

}

/**
 * 读文件
 * @param {*} filePath 路径
 */
function readFile(filePath){

  let content;

  try {

    content = fs.readFileSync(path.join(__dirname,filePath),'utf8');

  } catch (err) {
    
    message.error('读取文件错误'+err);

    process.exit();

  }

  return content

}

/**
 * 写文件
 * @param {*} filePath 文件路径
 * @param {*} content  内容
 * @param {*} filename 文件名
 */
function writeFile(filePath,content,filename){

  const fp = path.join(process.cwd(),filePath);

  fs.writeFile(fp,content,{encoding:'utf8'},function(err){

    if(err){

      message.error(`${filename}写入失败`);

    }else {

      message.success(`${filename}写入成功,文件路径:${fp}`);

    }

    process.exit();

  });

}

switch (argv.length) {

  case 3:

    processCommand(command1);

    break;

  default:

    break
}
module.exports = {
  length: argv.length, command1, command2, command3
}


